package auctionhaus

/**
 * Created with IntelliJ IDEA.
 * User: Chris Seeling
 * Date: 4/11/12
 * Time: 5:48 PM
 * To change this template use File | Settings | File Templates.
 */
class NamedQueryTests extends GroovyTestCase {
    def listing
    def seller

    public void setUp() {
        seller = new Customer(
                email: "johndoe@acme.com",
                password: "asdfwe",
                //role: "user"
        ).save(failOnError: true)
        listing = new Listing(name: "Listing 2",
                endTime: new Date()+2,
                description: "blah blah blah",
                seller: seller,
                startBid: 5,
                minBid: 5).save(failOnError: true)

        if(!listing.bids) {
            listing.bids = new HashSet<Bid>()
        }
        (0..<15).each {
            def newBid = new Bid(
                    amount: it + 5,
                    bidder:  seller
            )
            listing.bids.add(newBid)
            newBid.listing = listing
            listing.save(flush: true)
        }
    }

    void testRecentBidsQuery() {
        def bids = Bid.recentBids(listing).list(max: 10)
        assert bids.size() == 10
        /**
         * Bid amounts go from 5 to 19. The top ten bids should be 10 to 19
         */
        bids.each {
            assert it.amount >= 10
        }
    }

    public void tearDown() {
        seller = null
        listing = null
    }
}
